Imports System.Text

Friend Class DBReportSchedule_Factory
    Inherits DBBase

    Public Function GetReportForSystem() As ReportSchedule
        Dim oReportSchedule As ReportSchedule = Nothing

        Dim sSQL As New StringBuilder("select")
        With sSQL
            .Append(" [id]")
            .Append(" from")
            .Append(" ReportSchedule")
            .Append(" where")
            .Append(" serverid is null")
            .Append(" and databaseid is null")
        End With

        Dim oValue As Object = MyBase.ExecuteScalar(sSQL.ToString)
        If Not oValue Is Nothing Then
            oReportSchedule = New ReportSchedule(Val(oValue))
            oReportSchedule.Load()
        End If

        Return oReportSchedule
    End Function

    Public Function GetReportForServer(ByVal lServerID As Long) As ReportSchedule
        Dim oReportSchedule As ReportSchedule = Nothing

        Dim sSQL As New StringBuilder("select")
        With sSQL
            .Append(" [id]")
            .Append(" from")
            .Append(" ReportSchedule")
            .Append(" where")
            .Append(" serverid =" & lServerID)
            .Append(" and databaseid is null")
        End With

        Dim oValue As Object = MyBase.ExecuteScalar(sSQL.ToString)
        If Not oValue Is Nothing Then
            oReportSchedule = New ReportSchedule(Val(oValue))
            oReportSchedule.Load()
        End If

        Return oReportSchedule
    End Function

    Public Function GetReportForDatabase(ByVal lDatabaseID As Long) As ReportSchedule
        Dim oReportSchedule As ReportSchedule = Nothing

        Dim sSQL As New StringBuilder("select")
        With sSQL
            .Append(" [id]")
            .Append(" from")
            .Append(" ReportSchedule")
            .Append(" where")
            .Append(" databaseid = " & lDatabaseID)
        End With

        Dim oValue As Object = MyBase.ExecuteScalar(sSQL.ToString)
        If Not oValue Is Nothing Then
            oReportSchedule = New ReportSchedule(Val(oValue))
            oReportSchedule.Load()
        End If

        Return oReportSchedule
    End Function

    Public Function GetNextScheduledReport() As ReportSchedule
        Dim oReportSchedule As ReportSchedule = Nothing

        Dim sSQL As New StringBuilder("select top 1")
        With sSQL
            .Append(" [id]")
            .Append(" from")
            .Append(" ReportSchedule a")
            .Append(" where")
            .Append(" [IsActive]=1")
            .Append(" and ([lastreportcompiledate] is null")
            .Append("       or datediff(minute,[lastreportcompiledate],current_timestamp)>=60)")
            .Append(" and ([lastreportsenddate] is null")
            .Append("       or datediff(day,[lastreportsenddate],current_timestamp)>=[delaydays])")
            .Append(" order by")
            .Append(" isnull([changedate],[createdate])")
        End With

        Dim oValue As Object = MyBase.ExecuteScalar(sSQL.ToString)
        If Not oValue Is Nothing Then
            oReportSchedule = New ReportSchedule(Val(oValue))
            oReportSchedule.Load()
        End If

        Return oReportSchedule
    End Function
End Class
